﻿using System.Drawing;
using System.IO;

namespace XYAC.Framework.MonitorTireMaterialManual.DataExporter
{
    internal class LaserDataExporter
    {
        private float m_FileInertvaWidth;
        public MeasurementDataResult ReadData(string FileName)
        {
            var result = new MeasurementDataResult();

            try
            {
                using (StreamReader sr = new StreamReader(FileName))
                {
                    string str;
                    string[] s;
                    string strSection = "[Points]";
                    bool isFirstLine = true;
                    int TypeID = 0;
                    List<Measuring.MeasurData> m_listdata = new List<Measuring.MeasurData>();
                    List<PointF> listMeasureKey = new List<PointF>();
                    float XReference = 0;
                    string typename = "";

                    while ((str = sr.ReadLine()) != null)
                    {
                        if (string.IsNullOrWhiteSpace(str)) break;

                        if (isFirstLine) // 如果是第一行，读取间距
                        {
                            isFirstLine = false;
                            m_FileInertvaWidth = Convert.ToSingle(str);
                            result.FileInertvaWidth = m_FileInertvaWidth;
                        }
                        else
                        {
                            switch (str)
                            {
                                case "[Keys]":
                                case "[XReference]":
                                case "[MeasureKeys]":
                                case "[TypeName]":
                                case "[ID]":
                                    strSection = str;
                                    continue;
                            }

                            switch (strSection)
                            {
                                case "[Points]":
                                    s = str.Split();
                                    if (s.Length >= 3)
                                    {
                                        float x = float.Parse(s[1]);
                                        float y = float.Parse(s[2]);
                                        var measurData = new Measuring.MeasurData(x, y);
                                        m_listdata.Add(measurData);
                                    }
                                    break;
                                case "[Keys]":
                                    // 处理 [Keys] 部分的数据
                                    break;
                                case "[XReference]":
                                    XReference = float.Parse(str);
                                    result.XReference = XReference;
                                    break;
                                case "[MeasureKeys]":
                                    s = str.Split();
                                    if (s.Length >= 2)
                                    {
                                        float keyX = float.Parse(s[0]);
                                        float keyY = float.Parse(s[1]);
                                        listMeasureKey.Add(new PointF(keyX, keyY));
                                    }
                                    break;
                                case "[TypeName]":
                                    typename = str;
                                    result.TypeName = typename;
                                    break;
                                case "[ID]":
                                    TypeID = Convert.ToInt32(str);
                                    result.TypeID = TypeID;
                                    break;
                            }
                        }
                    }

                    // 设置返回结果的其他属性
                    result.ListData = m_listdata;
                    result.ListMeasureKey = listMeasureKey;
                    result.Nominal = new ClsMyNominal(typename);
                    result.MeasuringData = new ClsMyMeasuring(m_listdata, m_FileInertvaWidth)
                    {
                        XReference = XReference
                    };

                }
            }
            catch (Exception ex)
            {
                // 处理异常，例如记录日志
                // 根据需求，您可以返回 null 或者一个包含错误信息的对象
                return null;
            }

            return result;
        }


        private bool SaveAsFile(MeasurementDataResult dataResult, string fileName)
        {
            try
            {
                string sFileName = "";
                //System.IO.DirectoryInfo file = new System.IO.DirectoryInfo(filePath);
                //if (!file.Exists)
                //    file.Create();

                sFileName = fileName;
                System.IO.StreamWriter sw = new System.IO.StreamWriter(sFileName);
                //sw.WriteLine(m_IntervalWidth);
                for (int i = 0; i < dataResult.MeasuringData.ThicknessPoints.Count; i++)
                {
                    //起始点索引+上传感器内码值+上传感器测量值+下传感器内码值+下传感器测量值
                    string s = (i + "\t" + dataResult.MeasuringData.ThicknessPoints[i].Point.Y + "\t" + dataResult.MeasuringData.BottomHeightPoints[i].Point.Y);//HeightFloatList[i].ToString()
                    sw.WriteLine(s);
                }
                sw.WriteLine("[MeasureKeys]");
                for (int i = 0; i < dataResult.MeasuringData.KeyPoints.Count; i++)
                {
                    sw.WriteLine(dataResult.MeasuringData.KeyPoints[i].Point.X.ToString() + "\t" + dataResult.MeasuringData.KeyPoints[i].Point.Y.ToString());
                }
                sw.WriteLine("[TypeName]");
                sw.WriteLine(dataResult.Nominal.Name);
                sw.WriteLine("[ID]");
                sw.WriteLine(dataResult.Nominal.DBID.ToString());
                sw.WriteLine("[XReference]");
                sw.WriteLine(dataResult.MeasuringData.XReference.ToString());
                sw.WriteLine("[Keys]");
                for (int i = 0; i < dataResult.Nominal.KeyPoints.Count; i++)
                {
                    sw.WriteLine(dataResult.Nominal.KeyPoints[i].Point.X.ToString() + "\t" + dataResult.Nominal.KeyPoints[i].Point.Y.ToString());
                }
                sw.Flush();
                sw.Close();
                return true;
            }
            catch (Exception ex)
            {
                //MessageBox.Show("(Err -001：uctMeasure,SaveAsFile)" + ex.Message.ToString());
                return false;
            }
        }
    }
}
